/**
 * Created by qiang on 2017-03-20 .
 */
// sessionStorage.token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvcGVuaWQiOiJvLV92dDBhRVBJUzBHU1kzandSeHBXY2w0ay1VIiwiYWNjZXNzX3Rva2VuIjoiNl80RDl3VmdzZjlSUktUSmpfcEF6eVB1NUJRMXJWS3MzR1dRelVWRUpMbkNBX1ZfeVBKanpjUlFjOWx2dHBlWWdmUlcxWDdUeXl0MW1SdVIwTkZ3dXJHZyIsInJlZnJlc2hfdG9rZW4iOiI2X3NKY1JFT3ZRVVZicEl6TFlxemkyRUVkYThpWXVYaHNMdjFkMTNtcXNGNGNkbXE0Z0k5QkN3eEVxMFlLT3cyX0JvNE5zMkxRMHZodWxWMVpoNElQTVpRIiwiZXhwaXJlc19pbiI6MTUxNzQ3MTA1NzI5OCwiaW52YWxpZF9pbiI6MTUxNzcyMzA1NzI5OCwiaWF0IjoxNTE3NDYzODU3fQ.KQileIKSKnDajANKE7fcUiEItoIXqxG2iOeROIF3YTo'
// sessionStorage.userInfo = '{"openid":"o-_vt0aEPIS0GSY3jwRxpWcl4k-U","nickname":"咩呀。","sex":1,"language":"zh_CN","city":"厦门","province":"福建","country":"中国","headimgurl":"http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLv9HJIW4OACo6TRPt4RzUXW3ibfMkoNzugfgANjLYyY1HKvL00ScusibYbVOtvmu9QZv4dh5ricD8Ww/132","privilege":[],"unionid":"opxeQ0qBW81vnR4481DgIhqJ59z0"}'
var self;
var params = common.getParamsFromURL(window.location)
document.title = params.userName;
var vm = new Vue({
    el: "#container",
    data: {
        chatNo: params.chatNo,
        toOpenId: params.openid,
        chatContent: '',
        recordList: [],
        userInfo: {},
        activeStarIndex: 0,
        recordCount: 0,
        TaReplyNum: 0,
        EvaluatePoint: 0,
        scroll: {},
        showFunc: false,
        showEvolution: false,
        editting: false,
        lastChatContentId: ''
    },
    created: function () {
        self = this;
    },
    mounted: function () {
        $(window).resize(function () {
            self.scroll.refresh();
        })
        common.auth(function () {
            if (sessionStorage.getItem("IsNewUser") != 'T') {
                self.getUserDetail(self.getRecoedListByCahtNo);
                setInterval(function () {
                    self.getRecoedListByCahtNo()
                }, 5000)
            } else {
                weui.alert("请先完成注册", function () {
                    location.href = 'userInfo.html';
                })
            }
        });

    },
    watch: {
        chatContent: function (val) {
            this.editting = val.length > 0
        }
    },
    methods: {
        jumpToUserDetail: function (openid) {
            /*if (self.userInfo.openid !== openid && params.IsBlack !== 'null') {
                weui.alert("ta已拉黑你，无法查看");
            } else {
                location.href = 'userInfo.html' + (self.userInfo.openid == openid ? '' : '?toOpenId=' + self.toOpenId);
            }*/
            location.href = 'userInfo.html' + (self.userInfo.openid == openid ? '' : '?toOpenId=' + self.toOpenId);
        },
        addStar: function (index) {
            this.activeStarIndex = index
        },
        formatDate: function (date) {
            if (new Date(date).toDateString() === new Date().toDateString()) {
                return new Date(date).Format("hh:mm") || ''
            } else if (new Date(date) < new Date()) {
                return new Date(date).Format("yyyy-MM-dd hh:mm") || ''
            } else {
                return ''
            }
        },
        getUserDetail: function (callback) {
            service.getUserDetail({}, function (data) {
                console.log(data)
                if (data && data.userInfo) {
                    self.userInfo = data.userInfo;
                }
                if (callback != undefined) {
                    callback();
                }
            })
        },
        createChat: function () {
            if (params.IsBlack === 'T' || (params.IsBlack !== 'null' && params.IsBlack !== 'F')) {
                weui.alert("ta已拉黑你，无法发送消息");
                return
            }
            if (params.BeBlack && params.BeBlack !== 'null') {
                weui.alert("你已拉黑ta，无法发送消息");
                return
            }
            if (!self.chatContent.trim()) {
                weui.alert("请先输入要发送的内容!");
                return
            }
            var createChatCallBack = function (res) {
                self.chatContent = ''
                self.getRecoedListByCahtNo();
            };
            var options = {
                ChatNo: self.chatNo,
                ToOpenId: self.toOpenId,
                ChatContent: self.chatContent
            };
            service.createChat(options, createChatCallBack);
        },
        evaluateUser: function () {
            if (this.activeStarIndex === 0) {
                weui.alert('请先进行评价！');
                return;
            }
            var evaluateUserCallBack = function (res) {
                weui.toast('评价成功', {duration: 300})
                self.showEvolution = false
            };
            var options = {
                Sincerity: self.activeStarIndex * 20,
                TaOpenId: self.toOpenId,
                EvaluatePoint: self.recordCount
            };
            service.evaluateUser(options, evaluateUserCallBack);
        },
        scrollToBottom: function () {
            self.scroll.scrollTo(0, self.scroll.maxScrollY);
        },
        getRecoedListByCahtNo: function (autoLoadFlag) {
            var getRecoedListByCahtNoCallBack = function (res) {
                console.log(res)
                var oldMaxScrollY = self.scroll.maxScrollY;
                if (res.data.length > 0) {
                    self.lastChatContentId = res.data[res.data.length - 1].ChatContentId
                }
                self.recordList = self.recordList.concat(res.data);
                self.recordCount += res.count;
                self.TaReplyNum = res.TaReplyNum;
                self.EvaluatePoint = res.EvaluatePoint;
                self.$nextTick(function () {
                    common.lazyLoadImg();
                    if (!self.scroll.refresh) {
                        self.scroll = new BScroll('.wrapper', {
                            scrollY: true,
                            pullDownRefresh: true,
                            bounce: true,
                            scrollbar: true,
                            momentum: true
                        })
                    } else {
                        self.scroll.refresh();
                    }
                    console.log(self.scroll.y, oldMaxScrollY)
                    if (res.count > 0 && (self.scroll.y === 0 || self.scroll.y - oldMaxScrollY <= 200)) {
                        self.scrollToBottom();
                    }
                })
                $(".loading-bg").hide();
            }
            var options = {
                ChatNo: self.chatNo,
                TaOpenId: self.toOpenId,
                lastChatContentId: self.lastChatContentId,
                options: {
                    "skip": 0,
                    "limit": 999
                }
            };
            service.getRecoedListByCahtNo(options, getRecoedListByCahtNoCallBack);
        }
    }
})